package puzzle.projecteuler.p100;

import java.math.BigInteger;

public class Problem080 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int s = 0;
		for (int i = 1; i <= 100; i ++) {
			Float f = sqrt(i, 99);
			if (f.n > 0) {
				s += sum(f);
			}
		}
		System.out.println(s);
	}
	
	public static class Float {
		BigInteger m;
		int n;
		public Float(BigInteger m, int n) {
			this.m = m;
			this.n = n;
		}
		
		public String toString() {
			String str = this.m.toString();
			int k = str.length()-this.n;
			if (this.n == 0) {
				return str.substring(0,k);
			} else {
				return str.substring(0,k) + "." + str.substring(k);
			}
			 
		}
	}
	
	public static Float sqrt(long m, int r) {
		
		long s = (long)Math.sqrt(m);
		if (m == s*s) {
			return new Float(BigInteger.valueOf(s), 0);
		} else {
			int n = 0;
			BigInteger tt = BigInteger.valueOf(m);
			BigInteger t = BigInteger.valueOf(s);
			BigInteger ten = BigInteger.valueOf(10);
			while (n < r) {
				tt = tt.multiply(ten).multiply(ten);
				t = t.multiply(ten);
				while (t.multiply(t).compareTo(tt) < 0) {
					t = t.add(BigInteger.ONE);
				}
				t = t.subtract(BigInteger.ONE);
				n ++;
			}
			return new Float(t, r);
		}
	}

	public static long sum(Float f) {
		
		long s = 0;
		char[] cs = f.m.toString().toCharArray();
		for (char c: cs) {
			s += c-'0';
		}
		return s;
	}
}
